package com.xin.demo.strategy;

public class StrategySort<T> {
    // 此处通过 采用选择排序进行排序
    public void selectionSort(T[] arr, Comparator<T> comparator) {
        if (arr == null || arr.length < 2) {
            return;
        }
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                minIndex = comparator.compare(arr[j], arr[minIndex]) < 0 ? j : minIndex;
            }
            swap(arr, i, minIndex);
        }
    }

    public void swap(T[] arr, int i, int j) {
        T tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

}
